﻿

CREATE     FUNCTION [SoldDebit] (@functieCont as varchar(1), @debit as decimal(20, 2), @credit as decimal(20, 2))  
RETURNS decimal(20, 2) AS  
BEGIN 
	declare @sold as decimal(20, 2)
	if @functieCont = 'A' set @sold = COALESCE(@debit, 0) - COALESCE(@credit, 0)
	else
	if @functieCont = 'P' set @sold = 0
	else
	if (@functieCont = 'B') OR (@functieCont = 'X')
		if @debit >= @credit set @sold = COALESCE(@debit, 0) - COALESCE(@credit, 0)
		else set @sold =0

	return @sold
END